package simulation;

import streaming.Host;
import streaming.StreamingConfig;

/**
 * This class executes the periodic update event, i.e., the event
 * where the node update its information.
 * 
 * @author emanuel
 */

public class PeriodicUpdate implements EventHandler {

	Host host;
	Simulator sim;
	
	public PeriodicUpdate(Host newHost) {
		this.setHost(newHost);
		this.setSim(Simulator.getInstance());
	}
	
	public void addEvent() {
		double time = this.getSim().getTime() + this.getInterval();
		if (Math.abs(time - Math.round(time)) < 0.00001)
			time = Math.round(time);
		this.getSim().addEvent(time, this);
	}
	
	public void run(double time) {
		if (this.getHost().getId() == -1)
			return;
		this.getHost().update();
	}

	/* Getters and Setters */
	
	public Host getHost() {
		return host;
	}

	public void setHost(Host host) {
		this.host = host;
	}

	public Simulator getSim() {
		return sim;
	}

	public void setSim(Simulator sim) {
		this.sim = sim;
	}

	public double getInterval() {
		return StreamingConfig.MCASTUPDATEINTERVAL;
	}
}
